<template>
  <div class="dashboard">
    <h2 class="page-title">仪表盘</h2>
    
    <!-- 统计卡片 -->
    <el-row :gutter="20" class="stats-row">
      <el-col :xs="12" :sm="6" :md="6" :lg="6">
        <el-card class="stat-card student-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon><User /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ studentStore.totalStudents }}</div>
              <div class="stat-label">总学生数</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6" :md="6" :lg="6">
        <el-card class="stat-card class-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon><OfficeBuilding /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ appStore.classes.length }}</div>
              <div class="stat-label">班级数量</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6" :md="6" :lg="6">
        <el-card class="stat-card course-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon><Reading /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">{{ appStore.courses.length }}</div>
              <div class="stat-label">课程数量</div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="12" :sm="6" :md="6" :lg="6">
        <el-card class="stat-card grade-card" shadow="hover">
          <div class="stat-content">
            <div class="stat-icon">
              <el-icon><TrendCharts /></el-icon>
            </div>
            <div class="stat-info">
              <div class="stat-number">85%</div>
              <div class="stat-label">平均及格率</div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 性别分布图表 -->
    <el-row :gutter="20" class="charts-row">
      <el-col :xs="24" :sm="12" :md="12" :lg="12">
        <el-card class="chart-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>学生性别分布</span>
            </div>
          </template>
          <div class="gender-chart">
            <div class="gender-item male">
              <div class="gender-icon">
                <el-icon><Male /></el-icon>
              </div>
              <div class="gender-info">
                <div class="gender-count">{{ studentStore.maleStudents }}</div>
                <div class="gender-label">男生</div>
              </div>
            </div>
            <div class="gender-item female">
              <div class="gender-icon">
                <el-icon><Female /></el-icon>
              </div>
              <div class="gender-info">
                <div class="gender-count">{{ studentStore.femaleStudents }}</div>
                <div class="gender-label">女生</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      
      <el-col :xs="24" :sm="12" :md="12" :lg="12">
        <el-card class="chart-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>最近操作</span>
            </div>
          </template>
          <div class="recent-actions">
            <el-timeline>
              <el-timeline-item timestamp="2024-01-15 10:30" placement="top">
                <el-card>
                  <h4>添加新学生</h4>
                  <p>成功添加学生"张三"到计算机科学与技术1班</p>
                </el-card>
              </el-timeline-item>
              <el-timeline-item timestamp="2024-01-15 09:15" placement="top">
                <el-card>
                  <h4>更新课程信息</h4>
                  <p>更新了"数据库原理"课程的学分信息</p>
                </el-card>
              </el-timeline-item>
              <el-timeline-item timestamp="2024-01-14 16:45" placement="top">
                <el-card>
                  <h4>成绩录入</h4>
                  <p>批量录入"高等数学"期末考试成绩</p>
                </el-card>
              </el-timeline-item>
            </el-timeline>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 班级概况 -->
    <el-row class="class-overview">
      <el-col :span="24">
        <el-card class="overview-card" shadow="hover">
          <template #header>
            <div class="card-header">
              <span>班级概况</span>
              <el-button type="primary" size="small" @click="$router.push('/classes')">
                查看详情
              </el-button>
            </div>
          </template>
          <el-row :gutter="16">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" v-for="cls in appStore.classes" :key="cls.id">
              <div class="class-item">
                <div class="class-name">{{ cls.name }}</div>
                <div class="class-info">
                  <span class="class-grade">{{ cls.grade }}级</span>
                  <span class="class-count">{{ cls.student_count }}人</span>
                </div>
              </div>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useStudentStore } from '../stores/student'
import { useAppStore } from '../stores/app'

const studentStore = useStudentStore()
const appStore = useAppStore()

onMounted(async () => {
  try {
    await Promise.all([
      studentStore.fetchStudents(),
      appStore.fetchClasses(),
      appStore.fetchCourses()
    ])
  } catch (error) {
    console.error('加载数据失败:', error)
  }
})
</script>

<style scoped>
.dashboard {
  padding: 0;
}

.page-title {
  margin-bottom: 20px;
  color: #303133;
  font-weight: 500;
}

.stats-row {
  margin-bottom: 20px;
}

.stat-card {
  border-radius: 8px;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-2px);
}

.stat-content {
  display: flex;
  align-items: center;
  padding: 10px 0;
}

.stat-icon {
  font-size: 32px;
  margin-right: 15px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.student-card .stat-icon {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.class-card .stat-icon {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.course-card .stat-icon {
  background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
  color: white;
}

.grade-card .stat-icon {
  background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
  color: white;
}

.stat-info {
  flex: 1;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #303133;
  margin-bottom: 4px;
}

.stat-label {
  font-size: 12px;
  color: #909399;
}

.charts-row {
  margin-bottom: 20px;
}

.chart-card {
  border-radius: 8px;
  min-height: 300px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.gender-chart {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 200px;
}

.gender-item {
  text-align: center;
  padding: 20px;
  border-radius: 8px;
  transition: background-color 0.3s ease;
}

.gender-item:hover {
  background-color: #f5f7fa;
}

.gender-item.male .gender-icon {
  color: #409EFF;
}

.gender-item.female .gender-icon {
  color: #F56C6C;
}

.gender-icon {
  font-size: 40px;
  margin-bottom: 10px;
}

.gender-count {
  font-size: 32px;
  font-weight: bold;
  margin-bottom: 8px;
}

.gender-label {
  font-size: 14px;
  color: #909399;
}

.recent-actions {
  max-height: 250px;
  overflow-y: auto;
}

.class-overview {
  margin-bottom: 20px;
}

.overview-card {
  border-radius: 8px;
}

.class-item {
  background: #f5f7fa;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 10px;
  text-align: center;
  transition: all 0.3s ease;
}

.class-item:hover {
  background: #e4e7ed;
  transform: translateY(-2px);
}

.class-name {
  font-weight: 500;
  margin-bottom: 8px;
  color: #303133;
}

.class-info {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #909399;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .stats-row .el-col {
    margin-bottom: 10px;
  }
  
  .stat-number {
    font-size: 20px;
  }
  
  .stat-icon {
    font-size: 24px;
    width: 40px;
    height: 40px;
  }
  
  .gender-count {
    font-size: 24px;
  }
  
  .gender-icon {
    font-size: 32px;
  }
}
</style> 